/*
 * Created: Author: Aléx Carvalho / Date: Feb 19, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-12 03:38:01 +0000 (Fri, 12 Jun 2015) $
 */
package com.cpm.view.zk.treetable.indicator;

import com.cpm.ServiceLocator;
import com.cpm.aggregate.SessionAttribute;
import com.cpm.context.SessionContext;
import com.cpm.controller.CompetenceController;
import com.cpm.entities.Competence;
import com.cpm.entities.Indicator;
import com.cpm.view.zk.field.DoubleField;
import com.cpm.wrapper.Result;
import com.cpm.wrapper.SolidObjects;
import java.time.Month;
import java.time.Year;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Treecell;
import org.zkoss.zul.Treerow;

public class IndicatorResultRow {

    private final Indicator indicator;
    private final Year year;

    public IndicatorResultRow(Indicator indicator) {
        SolidObjects.notNull(indicator);
        this.indicator = indicator;
        year = ServiceLocator.lookup(SessionContext.class).getAttribute(SessionAttribute.INDICATOR_TREE_TABLE_CURRENT_YEAR);
    }

    public void render(Treerow row, int i) {

        new Treecell().setParent(row);
        new Treecell("R").setParent(row);

        getResulField(indicator, Month.JANUARY).setParent(row);
        getResulField(indicator, Month.FEBRUARY).setParent(row);
        getResulField(indicator, Month.MARCH).setParent(row);
        getResulField(indicator, Month.APRIL).setParent(row);
        getResulField(indicator, Month.MAY).setParent(row);
        getResulField(indicator, Month.JUNE).setParent(row);
        getResulField(indicator, Month.JULY).setParent(row);
        getResulField(indicator, Month.AUGUST).setParent(row);
        getResulField(indicator, Month.SEPTEMBER).setParent(row);
        getResulField(indicator, Month.OCTOBER).setParent(row);
        getResulField(indicator, Month.NOVEMBER).setParent(row);
        getResulField(indicator, Month.DECEMBER).setParent(row);
    }

    private Treecell getResulField(Indicator indicator, Month month) {

        Competence c = indicator.getCompetenceList().getCompetenceByYearAndMonth(year, month);
        DoubleField field = new DoubleField();
        field.setValue(c.getResult());
        field.setWidth("98%");
        field.setStyle("text-align:center;");

        field.addEventListener(Events.ON_BLUR, e -> {
            Result result = c.getResult();
            Double newValue = field.getValue();

            if (result == null || result.getValue() == null || !result.getValue().equals(newValue)) {

                if (result == null && newValue == null) {
                    return;
                }
                
                c.setResult(newValue != null ? new Result(newValue) : null);

                ServiceLocator.lookup(CompetenceController.class).updateResult(c);
            }
        });

        Treecell cell = new Treecell();
        cell.appendChild(field.getElement());
        cell.setStyle("text-align: center; border-left: 2px solid rgb(167, 167, 167);");

        return cell;
    }
}
